package com.FrameWork.Common;

import java.net.InetSocketAddress;
import java.util.Calendar;

import com.FrameWork.Utils.Utils;
import com.SFA.R;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;




public class IAppImpl {
	String strmd5;
	String timemd5;
	private final String servicesNameSpace = "http://tempuri.org/";


	public static boolean isGprsNet(){  
		String proxyHost = android.net.Proxy.getDefaultHost();  
		return proxyHost==null;  
	}  


	public static HttpTransportSE getHttpTransportSE(String servicesURL){  
		HttpTransportSE ht;  
		if(!isGprsNet()){  
			java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress(android.net.Proxy.getDefaultHost(),android.net.Proxy.getDefaultPort()));  
			ht=new HttpTransportSE(p,servicesURL);  
		}else{  
			ht = new HttpTransportSE(servicesURL);  
		}  
		return ht;  
	}  


	public String GetResouceByURL(String URL,Context context,String MethodNamee, Object[] parm1,
			Object... parm2) {
		strmd5 = "";
		try {

			String methodName = MethodNamee;
			SoapPrimitive detail;
			Calendar c = Calendar.getInstance();
			timemd5 = c.get(Calendar.YEAR) + "." + (c.get(Calendar.MONTH) + 1)
			+ "." + c.get(Calendar.DAY_OF_MONTH) + "."
			+ c.get(Calendar.HOUR_OF_DAY) + "."
			+ c.get(Calendar.MINUTE) + "." + c.get(Calendar.SECOND);
			String SOAP_ACTION = "http://tempuri.org/"+ methodName;
			SoapObject request = new SoapObject(servicesNameSpace, methodName);
			//request.
			if (parm1 != null) {
				for (int i = 0; i < parm2.length; i++) {

					if(parm2[i]==null)
					{
						request.addProperty(parm1[i].toString(),"");
					}
					else
					{
						request.addProperty(parm1[i].toString(), parm2[i]);
						strmd5 += parm2[i];
					}
				}
			} 
			strmd5 += timemd5;



			strmd5 += context.getResources().getString(R.string.REQUESTKEY);


			Element[] header = new Element[1];
			header[0] = new Element().createElement(servicesNameSpace,"Authentication");
			Element header1 = new Element().createElement(servicesNameSpace, "UniqueKey");
			header1.addChild(Node.TEXT, timemd5);
			header[0].addChild(Node.ELEMENT, header1);
			Element header2 = new Element().createElement(servicesNameSpace, "RequestKey");
			header2.addChild(Node.TEXT, Utils.GetStringForMD5(strmd5));
			header[0].addChild(Node.ELEMENT, header2);
		
			PackageManager pm = context.getPackageManager();
			String Version= "";
			try
			{
				PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
				Version=pi.versionName;
			}
			catch (NameNotFoundException e)
			{
				e.printStackTrace();
			}
			Element header3 = new Element().createElement(servicesNameSpace, "Version");
			header3.addChild(Node.TEXT, Version);
			header[0].addChild(Node.ELEMENT, header3);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.headerOut = header;
			envelope.bodyOut = request;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);
			HttpTransportSE ht = getHttpTransportSE(URL);

			ht.call(SOAP_ACTION, envelope);
			if(envelope.getResponse() != null && envelope.getResponse().getClass() == SoapObject.class &&  ((SoapObject)envelope.getResponse()).getPropertyCount()==0)
				return null;
			detail = (SoapPrimitive) envelope.getResponse();

			return detail.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


}
